#ifndef MYFUNCTION_H
#define MYFUNCTION_H

#include <string>
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

class MyFunction
{
public:
	static string IntToString(int number)
	{
		stringstream ss;
		ss << number;
		return ss.str();
	}
	static string FloatToString(float number)
	{
		stringstream ss;
		ss << number;
		return ss.str();
	}
	static string Char2String(char s)
	{	
		int i = s;
		if( i < 0 || i > 254)
			i += 256;
		return MyFunction::IntToString(i);
		
	}
	static int String2Int(string s)
	{	
		std::string a=s;
		std::istringstream b(a);
		int f;
		b >> f;
		return f;
	}	
	static int String2Float(string s)
	{	
		std::string a=s;
		std::istringstream b(a);
		int f;
		b >> f;
		return f;
	}	
	static double Char2Double(char s)
	{	
		int i = s;
		if( i < 0 || i > 254)
			i += 256;
		double d = i/255.0;
		return d;		
	}

	static char* ParseColor(string s) {
		if(s == "none") {
			return NULL;
		}

		int firstSemicolon = s.find_first_of(',');
		int lastSemicolon = s.find_last_of(',');
		int lastParenthesis = s.find_last_of(')');
		int red = MyFunction::String2Int(s.substr(4, firstSemicolon-4));			
		int green = MyFunction::String2Int(s.substr(firstSemicolon+1, lastSemicolon-firstSemicolon-1));
		int blue = MyFunction::String2Int(s.substr(lastSemicolon+1, lastParenthesis-lastSemicolon-1));
		
		char* stroke = new char[3];
		stroke[0] = red;
		stroke[1] = green;
		stroke[2] = blue;
		return stroke;
	}
};
#endif